from django.db import models
from django.contrib.auth.models import User

class Business(models.Model):
    """业务模型"""
    bk_biz_id = models.IntegerField(primary_key=True)
    bk_biz_name = models.CharField(max_length=100)
    bk_biz_developer = models.CharField(max_length=100)
    bk_biz_maintainer = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = '业务'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.bk_biz_name

class Cluster(models.Model):
    """集群模型"""
    name = models.CharField(max_length=100)
    region = models.CharField(max_length=50)
    status = models.CharField(max_length=20)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = '集群'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Node(models.Model):
    """节点模型"""
    name = models.CharField(max_length=100)
    cluster = models.ForeignKey(Cluster, on_delete=models.CASCADE, related_name='nodes')
    ip_address = models.GenericIPAddressField()
    status = models.CharField(max_length=20)
    cpu_usage = models.FloatField(default=0)
    memory_usage = models.FloatField(default=0)
    disk_usage = models.FloatField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = '节点'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Instance(models.Model):
    """实例模型"""
    name = models.CharField(max_length=100)
    node = models.ForeignKey(Node, on_delete=models.CASCADE, related_name='instances')
    status = models.CharField(max_length=20)
    cpu_usage = models.FloatField(default=0)
    memory_usage = models.FloatField(default=0)
    disk_usage = models.FloatField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = '实例'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class ResourceUsage(models.Model):
    """资源使用记录"""
    RESOURCE_TYPES = (
        ('cluster', '集群'),
        ('node', '节点'),
        ('instance', '实例'),
    )
    
    resource_type = models.CharField(max_length=10, choices=RESOURCE_TYPES)
    resource_id = models.IntegerField()
    cpu_usage = models.FloatField()
    memory_usage = models.FloatField()
    disk_usage = models.FloatField()
    timestamp = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = '资源使用记录'
        verbose_name_plural = verbose_name
        ordering = ['-timestamp']

class UserAction(models.Model):
    """用户行为记录"""
    ACTION_TYPES = (
        ('query', '查询'),
        ('create', '创建'),
        ('update', '更新'),
        ('delete', '删除'),
    )
    
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    action_type = models.CharField(max_length=10, choices=ACTION_TYPES)
    resource_type = models.CharField(max_length=20)
    resource_id = models.IntegerField()
    action_detail = models.JSONField()
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = '用户行为'
        verbose_name_plural = verbose_name
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username} - {self.action_type} - {self.resource_type}" 